package com.tomorrowbbs.service.impl;

import java.util.Date;
import java.util.List;
//import com.newcamping.common.utils.DateUtils;
import com.tomorrowbbs.domain.ForumArticle;
import com.tomorrowbbs.domain.ForumComment;
import com.tomorrowbbs.domain.UserMessage;
import com.tomorrowbbs.domain.constants.Constants;
import com.tomorrowbbs.domain.enums.MessageStatusEnum;
import com.tomorrowbbs.domain.enums.MessageTypeEnum;
import com.tomorrowbbs.domain.enums.OperRecordOpTypeEnum;
import com.tomorrowbbs.domain.enums.UpdateArticleCountTypeEnum;
import com.tomorrowbbs.exception.BusinessException;
import com.tomorrowbbs.mapper.ForumArticleMapper;
import com.tomorrowbbs.mapper.ForumCommentMapper;
import com.tomorrowbbs.mapper.UserMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tomorrowbbs.mapper.LikeRecordMapper;
import com.tomorrowbbs.domain.LikeRecord;
import com.tomorrowbbs.service.ILikeRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 点赞记录Service业务层处理
 * 
 * @author zjz
 * @date 2024-11-09
 */
@Service
public class LikeRecordServiceImpl implements ILikeRecordService 
{
    @Autowired
    private LikeRecordMapper likeRecordMapper;
    @Autowired
    private UserMessageMapper userMessageMapper;
    @Autowired
    private ForumArticleMapper forumArticleMapper;
    @Autowired
    private ForumCommentMapper forumCommentMapper;

    /**
     * 查询点赞记录
     * 
     * @param opId 点赞记录主键
     * @return 点赞记录
     */
    @Override
    public LikeRecord selectLikeRecordByOpId(Long opId)
    {
        return likeRecordMapper.selectLikeRecordByOpId(opId);
    }

    /**
     * 查询点赞记录列表
     * 
     * @param likeRecord 点赞记录
     * @return 点赞记录
     */
    @Override
    public List<LikeRecord> selectLikeRecordList(LikeRecord likeRecord)
    {
        return likeRecordMapper.selectLikeRecordList(likeRecord);
    }
    @Override
    public LikeRecord selectLikeRecord(LikeRecord likeRecord)
    {
        return likeRecordMapper.selectLikeRecord(likeRecord);
    }

    /**
     * 新增点赞记录
     * 
     * @param likeRecord 点赞记录
     * @return 结果
     */
    @Override
    public int insertLikeRecord(LikeRecord likeRecord)
    {
//        likeRecord.setCreateTime(DateUtils.getNowDate());
        return likeRecordMapper.insertLikeRecord(likeRecord);
    }

    /**
     * 修改点赞记录
     * 
     * @param likeRecord 点赞记录
     * @return 结果
     */
    @Override
    public int updateLikeRecord(LikeRecord likeRecord)
    {
        return likeRecordMapper.updateLikeRecord(likeRecord);
    }

    /**
     * 批量删除点赞记录
     * 
     * @param opIds 需要删除的点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteLikeRecordByOpIds(Long[] opIds)
    {
        return likeRecordMapper.deleteLikeRecordByOpIds(opIds);
    }

    /**
     * 删除点赞记录信息
     * 
     * @param opId 点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteLikeRecordByOpId(Long opId)
    {
        return likeRecordMapper.deleteLikeRecordByOpId(opId);
    }

    /*
     * 点赞*/

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doLike(String objectId, String userId, String nickName, OperRecordOpTypeEnum opType) {
        UserMessage userMessage = new UserMessage();
        userMessage.setCreateTime(new Date());

        switch (opType){
            case ARTICLE_LIKE:
                ForumArticle forumArticle = forumArticleMapper.selectForumArticleByArticleId(objectId);
                if (forumArticle == null){
                    throw new BusinessException("文章不存在");
                }

                articleLike(objectId,forumArticle, userId, opType);
                //更新数据
                userMessage.setArticleId(objectId);
                userMessage.setArticleTitle(forumArticle.getTitle());
                userMessage.setMessageType(MessageTypeEnum.ARTICLE_LIKE.getType());
                userMessage.setCommentId(0l);
                userMessage.setReceivedUserId(forumArticle.getUserId());
                break;
            case COMMENT_LIKE:
                ForumComment forumComment = forumCommentMapper.selectForumCommentByCommentId(Long.valueOf(objectId));
                if (forumComment == null){
                    throw new BusinessException("评论不存在");
                }
                commentLike(objectId,forumComment, userId, opType);

                forumArticle = forumArticleMapper.selectForumArticleByArticleId(forumComment.getArticleId());
                //更新数据
                userMessage.setArticleId(objectId);
                userMessage.setArticleTitle(forumArticle.getTitle());
                userMessage.setMessageType(MessageTypeEnum.ARTICLE_LIKE.getType());
                userMessage.setCommentId(forumComment.getCommentId());
                userMessage.setReceivedUserId(forumComment.getUserId());
                userMessage.setMessageContent(forumComment.getContent());
                break;
        }

        userMessage.setSendUserId(userId);
        userMessage.setSendNickName(nickName);
        userMessage.setStatus(MessageStatusEnum.NO_READ.getStatus());
        if (!userId.equals(userMessage.getReceivedUserId())){
            UserMessage userMessage1 = new UserMessage();
            userMessage1.setArticleId(userMessage.getArticleId());
            userMessage1.setCommentId(userMessage.getCommentId());
            userMessage1.setSendUserId(userMessage.getSendUserId());
            userMessage1.setMessageType(userMessage.getMessageType());

            UserMessage dbInfo = userMessageMapper.selectUserMessageByInfo(userMessage1);
            if (dbInfo == null) {
                userMessageMapper.insertUserMessage(userMessage);
            }
        }

    }

    public void articleLike (String objectId,ForumArticle forumArticle, String userId, OperRecordOpTypeEnum opType){
        LikeRecord likeRecord = new LikeRecord();
        likeRecord.setObjectId(objectId);
        likeRecord.setUserId(userId);
        likeRecord.setOpType(opType.getType());
        LikeRecord record = likeRecordMapper.selectLikeRecord(likeRecord);

        //已经点赞
        if (record != null){
            likeRecordMapper.deleteLikeRecordByCurrentInfo(likeRecord);
        }else {
            LikeRecord likeRecord1 = new LikeRecord();
            likeRecord1.setObjectId(objectId);
            likeRecord1.setUserId(userId);
            likeRecord1.setOpType(opType.getType());
            likeRecord1.setCreateTime(new Date());
            likeRecord1.setAuthorUserId(forumArticle.getUserId());
            likeRecordMapper.insertLikeRecord(likeRecord1);
            forumArticleMapper.updateArticleCount(UpdateArticleCountTypeEnum.GOOD_COUNT.getType(), Constants.ONE,objectId);
        }
    }

    public void commentLike (String objectId,ForumComment forumComment, String userId, OperRecordOpTypeEnum opType){
        LikeRecord likeRecord = new LikeRecord();
        likeRecord.setObjectId(objectId);
        likeRecord.setUserId(userId);
        likeRecord.setOpType(opType.getType());
        LikeRecord record = likeRecordMapper.selectLikeRecord(likeRecord);

        Integer changeCount = 0;
        if (record != null){
            changeCount = -1;
            likeRecordMapper.deleteLikeRecordByCurrentInfo(likeRecord);
        }else {


            changeCount = 1;
            LikeRecord likeRecord1 = new LikeRecord();
            likeRecord1.setObjectId(objectId);
            likeRecord1.setUserId(userId);
            likeRecord1.setOpType(opType.getType());
            likeRecord1.setCreateTime(new Date());
            likeRecord1.setAuthorUserId(forumComment.getUserId());
            likeRecordMapper.insertLikeRecord(likeRecord1);
        }
        forumCommentMapper.updateCommentGoodCount(changeCount,Long.valueOf(objectId));
    }





}













